package com.haskie.auth.controller;


import com.haskie.api.base.RespData;
import com.haskie.api.utils.RedisUtil;
import com.haskie.api.vo.req.LoginUserVoReq;
import com.haskie.auth.utils.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
@Api(value = "权限controller", tags = "用户权限操作接口")
public class LoginController {

    @Autowired
    RedisUtil redisUtil;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录接口", tags = "用户登录", notes = "jwt鉴权")
    @ApiImplicitParam(value = "用户登录请求VO", name = "loginUserVoReq", required = true, dataType = "loginUserVoReq")
    public RespData login(
            @RequestBody LoginUserVoReq loginUserVoReq
    ) {
        Map<String, Object> map = new HashMap<>();
        if ("admin".equals(loginUserVoReq.getUserName()) && "admin".equals(loginUserVoReq.getPassword())) {
            //生成token
            String token = JWTUtil.generateToken(loginUserVoReq.getUserName());
            map.put("token", token);
            //生成refreshToken
            String refreshToken = UUID.randomUUID().toString().replace("-", "");
            map.put("refreshToken", refreshToken);
        }

        return new RespData(map);
    }
}
